#!/usr/bin/env python3
"""
抓取真实的交易所Logo
"""
import requests
import os
from pathlib import Path

# Logo URLs - 使用CoinGecko, CryptoCompare等公开API或CDN
LOGO_SOURCES = {
    # 主流交易所的真实logo URLs
    'binance': 'https://assets.coingecko.com/markets/images/52/small/binance.jpg',
    'binanceus': 'https://assets.coingecko.com/markets/images/469/small/binance_us.jpg',
    'okx': 'https://assets.coingecko.com/markets/images/96/small/okx.jpg',
    'gateio': 'https://assets.coingecko.com/markets/images/60/small/gate.jpg',
    'bybit': 'https://assets.coingecko.com/markets/images/698/small/bybit.jpg',
    'kucoin': 'https://assets.coingecko.com/markets/images/61/small/kucoin.jpg',
    'huobi': 'https://assets.coingecko.com/markets/images/25/small/htx.jpg',
    'kraken': 'https://assets.coingecko.com/markets/images/29/small/kraken.jpg',
    'coinbase': 'https://assets.coingecko.com/markets/images/23/small/Coinbase_Coin_Primary.jpg',
    'mexc': 'https://assets.coingecko.com/markets/images/409/small/MEXC_Logo.jpg',
    'bingx': 'https://assets.coingecko.com/markets/images/780/small/bingx.jpg',
    'bitget': 'https://assets.coingecko.com/markets/images/540/small/2023-07-25_21.47.43.jpg',
    'bitmart': 'https://assets.coingecko.com/markets/images/239/small/bitmart.jpg',
    'bitrue': 'https://assets.coingecko.com/markets/images/302/small/bitrue.jpg',
    'bitstamp': 'https://assets.coingecko.com/markets/images/9/small/bitstamp.jpg',
    'crypto-dot-com': 'https://assets.coingecko.com/markets/images/589/small/crypto_dot_com.jpg',
    'ascendex': 'https://assets.coingecko.com/markets/images/97/small/ascendex.jpg',
    'btcmarkets': 'https://assets.coingecko.com/markets/images/151/small/btcmarkets.jpg',
    'dexalot': 'https://assets.coingecko.com/coins/images/28605/small/dexalot.png',
    'dydx': 'https://assets.coingecko.com/coins/images/17500/small/dydx.png',
    'foxbit': 'https://assets.coingecko.com/markets/images/463/small/foxbit.jpg',
    'hyperliquid': 'https://assets.coingecko.com/coins/images/40499/small/hyperliquid.jpg',
    'injective': 'https://assets.coingecko.com/coins/images/12882/small/injective.png',
    'ndax': 'https://assets.coingecko.com/markets/images/526/small/ndax.jpg',
    'vertex-protocol': 'https://assets.coingecko.com/coins/images/29504/small/vertex.png',
    'xrp': 'https://assets.coingecko.com/coins/images/44/small/xrp.png',
    'deriv': 'https://deriv.com/favicons/favicon-32x32.png',
}

def download_logo(name, url, output_dir):
    """下载并保存logo"""
    try:
        print(f"正在下载 {name}...")
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        
        # 确定文件扩展名
        content_type = response.headers.get('content-type', '')
        if 'svg' in content_type:
            ext = 'svg'
        elif 'png' in content_type or url.endswith('.png'):
            ext = 'png'
        else:
            ext = 'jpg'
        
        # 保存文件
        output_path = output_dir / f"{name}.{ext}"
        with open(output_path, 'wb') as f:
            f.write(response.content)
        
        print(f"✓ {name} 下载完成 ({ext})")
        return True
    except Exception as e:
        print(f"✗ {name} 下载失败: {e}")
        return False

def main():
    # 设置输出目录
    script_dir = Path(__file__).parent
    project_dir = script_dir.parent
    output_dir = project_dir / 'assets' / 'icons' / 'exchanges'
    output_dir.mkdir(parents=True, exist_ok=True)
    
    print(f"输出目录: {output_dir}")
    print(f"开始下载 {len(LOGO_SOURCES)} 个交易所logo...\n")
    
    success_count = 0
    for name, url in LOGO_SOURCES.items():
        if download_logo(name, url, output_dir):
            success_count += 1
    
    print(f"\n完成! 成功下载 {success_count}/{len(LOGO_SOURCES)} 个logo")

if __name__ == '__main__':
    main()

